//
//  SISSHomeRenantViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/14.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHomeTenantViewController.h"
#import "SISSCommonRentCell.h"
#import "SISSTenantDetailViewController.h"
#import "SISSRecommendRentOutViewController.h"
#import "SISSFindTenantViewController.h"
#import "SISSTenantModel.h"
#import "SISSNetworkingManager+Home.h"

static NSString * const tenantCellReuseID = @"tenantCellReuseID";

@interface SISSHomeTenantViewController ()<SISSCommonRentCellDelegate>

/** 租客数组 */
@property (strong, nonatomic) NSArray<SISSTenantModel *> *tenantArr;

@end

@implementation SISSHomeTenantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestNewestRenantResourceData];
    
    // 去掉系统自带底部的线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 自动行高
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    // 注册cell
    [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:tenantCellReuseID];
}

//- (void)setCanScroll:(BOOL)canScroll {
//    _canScroll = canScroll;
//    self.tableView.scrollEnabled = canScroll;
//}


#pragma mark - UITableViewDataSource
// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"没有找到相关出租！" ifNecessaryForRowCount:self.tenantArr.count];
    if (self.tenantArr.count > 0) {
        return self.tenantArr.count + 1;
    }
    return self.tenantArr.count;
}


// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:tenantCellReuseID forIndexPath:indexPath];
    
    cell.delegate = self;
    
    if (indexPath.row == self.tenantArr.count) {
        static NSString * const reuseID = @"ID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
            UIButton *view = [[UIButton alloc] initWithFrame:cell.contentView.bounds];
            [cell.contentView addSubview:view];
            
            UIButton *button = [[UIButton alloc] init];
            [button setTitle:@"查看更多房源" forState:UIControlStateNormal];
            [button setTitleColor:COLOR(0x8998A5) forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:13];
            [button addTarget:self action:@selector(jumpToHouseResourceList) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:button];
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(cell.contentView);
            }];
        }
        return cell;
    }
    cell.tenantModel = self.tenantArr[indexPath.row];
    
    return cell;
}

- (void)jumpToHouseResourceList {
    
    SISSFindTenantViewController *vc = [[SISSFindTenantViewController alloc] init];
    
    vc.title = @"找租客";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 去除tableView的选中状态
    SISSTenantDetailViewController *vc = [[SISSTenantDetailViewController alloc] init];
    vc.title = @"租客详情";
    SISSTenantModel *tenantModel = self.tenantArr[indexPath.row];
    vc.tenantModel = tenantModel;
    if (tenantModel.read != 1) {
        
        [kNetworkManager setTenantResourceReadWithTenantId:tenantModel.ID success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"插入成功！");
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
    self.tenantArr[indexPath.row].read = 1;
    [self.tableView reloadData];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0 && self.isForbiddenBlock) {
        self.isForbiddenBlock(NO);
    }
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.row == 0 && self.isForbiddenBlock) {
        self.isForbiddenBlock(YES);
    }
}

#pragma mark - SISSCommonRentCellDelegate
- (void)commonRentCellRecommendButtonDidClick:(UIButton *)button {
    
    SISSCommonRentCell *cell = (SISSCommonRentCell *)button.nextResponder.nextResponder;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSTenantModel *model = self.tenantArr[indexPath.row];
    
    SISSRecommendRentOutViewController *vc = [[SISSRecommendRentOutViewController alloc] init];
    vc.title = @"选择推荐租房";
    vc.arrId = model.ID;
    vc.recommendSuccessBlock = ^{
        [self requestNewestRenantResourceData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - 上下拉刷新加载方法
- (void)upDownReloadMoreData {
    
    [self requestNewestRenantResourceData];
}




#pragma mark - 2.1.1 首页20条最新租客
- (void)requestNewestRenantResourceData {
    
    WeakSelf
    [SISSPublicSingleton shared].isHud = YES;
    [kNetworkManager requestNewestTenantResourceWithSuccess:^(NSArray<SISSTenantModel *> *tenantResources) {

        weakSelf.tenantArr = tenantResources;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            [weakSelf.tableView reloadData];

            [weakSelf endHeaderRefresh];
        }];

    } failed:^(NSError *error) {
        [weakSelf endHeaderRefresh];
        NSLog(@"%@",error);
    }];
}

@end
